<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Author @liuhuiwu<13750088090@sina.cn>
 */
$code = $_GET['code'];
$appid = 'wx5fdcb8178b872984';
$appsecert = 'cae9d65fb19b69f3db1be303137b24ac';
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code";

//curl模拟get请求，获取结果
function http_curl($url){
    $curlobj = curl_init();
    curl_setopt($curlobj, CURLOPT_URL, $url);
    curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);

    $output = curl_exec($curlobj);
    curl_close($curlobj);

    return $output;
}
$res = http_curl($url);


//转化为数组
$return_data = json_decode($res,true);
//获取用户基本信息
$openid = $return_data['openid'];
$access_token = $return_data['access_token'];
$getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$userInfo = http_curl($getInfoUrl);
$weixins = json_decode($userInfo);

require_once 'common.inc.php';
if($openid){
    $query=$db->query("select uid from {$tablepre}members where password='{$openid}' limit 1");
    if(!$db->num_rows($query)) {
        $regtime=gdate();
        if($cfg['config']['regaudit']=='1')
            $state='0';
        else
            $state='1';
        $db->query("insert into {$tablepre}members(username,password,sex,email,regdate,regip,lastvisit,lastactivity,gold,realname,gid,phone,fuser,tuser,state)  values('$weixins->nickname','$weixins->openid','2','','$regtime','$onlineip','$regtime','$regtime','0','','1','','$tuser','$tuser','$state')");
        $uid=$db->insert_id();
        $db->query("replace into {$tablepre}memberfields (uid,nickname) values('$uid','$weixins->nickname') ");
        $db->query("insert into  {$tablepre}msgs(rid,ugid,uid,uname,tuid,tname,mtime,ip,msg,type) values('{$cfg[config][id]}','1','{$uid}','{$weixins->nickname}','{$cfg[config][defvideo]}','{$cfg[config][defvideonick]}','".gdate()."','{$onlineip}','用户注册','2')");
        $cache = file_get_contents($weixins->headimgurl);
        file_put_contents('../../../../face/p1/' . $uid . '.gif', $cache);
    }
}


/* 
**  php分别模拟发送GET与POST请求 
** 
*/ 
 
function httpRequest($url,$method,$params=array()){ 
    if(trim($url)==''||!in_array($method,array('get','post'))||!is_array($params)){ 
        return false; 
    } 
    $curl=curl_init(); 
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl,CURLOPT_HEADER,0 ) ; 
    switch($method){ 
        case 'get': 
            $str='?'; 
            foreach($params as $k=>$v){ 
                $str.=$k.'='.$v.'&'; 
            } 
            $str=substr($str,0,-1); 
            $url.=$str;//$url=$url.$str; 
            curl_setopt($curl,CURLOPT_URL,$url); 
        break; 
        case 'post': 
            curl_setopt($curl,CURLOPT_URL,$url); 
            curl_setopt($curl,CURLOPT_POST,1 ); 
            curl_setopt($curl,CURLOPT_POSTFIELDS,$params); 
        break; 
        default: 
            $result=''; 
        break; 
    } 
    $result=curl_exec($curl); 
    curl_close($curl); 
    return $result; 
}
// 用户中心第三方登录201611116 Author @liuhuiwu<13750088090@sina.cn>
$urlLogin = $ucurl . '/user/thirdLogin';
$params = array(
    'version' => '1.0.1',
    'user_id' => $openid,
    'username' => $weixins->nickname,
    'head_icon' => $weixins->headimgurl,
    'plat_from' => 'WEIXIN',
    'login_time' => time(),
    'device_id' => 'c92be85cb5c5ec5aea0d28f3f524d44274fa9aee1409285e23e7af015ed6b885',
    'device_from' => 'livePC',
    'third_num' => '513730844',
);
$res_data = httpRequest($urlLogin, $method='get', $params);
$result = json_decode($res_data,true);

$u = $weixins->nickname;
qc_login($u, $openid, 2);
header("location:" . $_SERVER['HOST_NAME'] . "/room");
